package club.yuit.desktop.ui.brokers;

import club.yuit.desktop.ui.MainUI;
import club.yuit.rmq.model.BrokerDetails;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Map;

/**
 * @author: yuit
 * @date: 2023/12/08 23:19
 */
public class BrokerCardPanel  extends JPanel {

    private BrokerDetails details;

    private JPopupMenu menu;

    public BrokerCardPanel(BrokerDetails details) {
        this.details = details;
        this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));

        this.add(titlePanel());
        this.add(new JSeparator());
        this.add(contentPanel());
        this.add(new JSeparator());
        this.add(optionsPanel());
        this.setBorder(new LineBorder(Color.GRAY,1,true));
        menu = new JPopupMenu();
        JMenuItem cfgItem = new JMenuItem("配置信息");
        JMenuItem statItem = new JMenuItem("运行状态");
        menu.add(cfgItem);
        menu.add(statItem);

        cfgItem.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                Container topLevelAncestor = getTopLevelAncestor();
                if (topLevelAncestor instanceof MainUI mainUI){
                    try {
                        Map<String, String> brokerConfig = mainUI.getService().getBrokerConfig(details.getAddr());
                        BrokerKVTableDialog dialog =  BrokerKVTableDialog.create(mainUI, "配置信息["+details.getName()+"]", brokerConfig);
                        dialog.showDialog();
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }

                }
            }
        });


        statItem.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                Container topLevelAncestor = getTopLevelAncestor();
                if (topLevelAncestor instanceof MainUI mainUI){
                    BrokerKVTableDialog dialog =  BrokerKVTableDialog.create(mainUI, "运行状态["+details.getName()+"]", details.getDetails());
                    dialog.showDialog();
                }
            }
        });



    }


    private JPanel titlePanel() {
        JPanel titlePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JLabel titleLabel = new JLabel(details.getName());
        titlePanel.add(titleLabel);
        return titlePanel;
    }

    public BrokerDetails getDetails() {
        return details;
    }

    private JPanel contentPanel() {
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        Box box = Box.createVerticalBox();

        JLabel versionLabel = new JLabel("版本：" + details.getVersionDec());

        JLabel ipLabel = new JLabel("地址：" + details.getAddr());

        Long id = details.getId();
        String idStr = id +"[slave]";
        if (id==0){
            idStr = id +"[master]";
        }
        JLabel idLabel = new JLabel("ID：" + idStr);

        JLabel clusterLabel = new JLabel("集群：" + details.getCluster());
        box.add(clusterLabel);
        box.add(versionLabel);
        box.add(ipLabel);
        box.add(idLabel);
        panel.add(box);
        return panel;
    }


    private JPanel optionsPanel() {
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        JButton btn = new JButton("更多");
        panel.add(btn);

        btn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                if (!menu.isShowing()){
                    Component component = e.getComponent();
                    Dimension minimumSize = component.getMinimumSize();
                    int height = minimumSize.height;
                    if (height<component.getHeight()){
                        height = component.getHeight();
                    }
                    menu.show(btn,e.getX(),component.getY()+height);
                }
            }
        });
        return panel;
    }

    @Override
    public Insets getInsets() {
        return new Insets(5,5,5,5);
    }

    public static BrokerCardPanel create(BrokerDetails details){
        return new BrokerCardPanel(details);
    }





}
